<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// +----------------------------------------------------------------------
// | 网站后台菜单管理
// +----------------------------------------------------------------------
// | Copyright (c) 2015
// +----------------------------------------------------------------------
// | Author: baiping 125618036@qq.com http://www.webipcode.com
// +----------------------------------------------------------------------
// | 2015-7-21下午4:14:07
// +----------------------------------------------------------------------

class Menu extends MY_Controller{
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 菜单首页
	 */
	public function index(){
		//获取pid
		$pid = intval( $this->uri->segment(3) );
		$where = array('pid'=>$pid);
		//搜索
		$search_name = $this->input->get_post('search_name');
		if ( !empty( $search_name ) && !is_numeric( $search_name )  ){
			$where = "title LIKE '%$search_name%' AND pid = ".$pid ;
			$suffix = urldecode( '?search_name='.$search_name );//如果url中除了页码参数还有其它参数时
		}
		
		//载入网站配置模型
		$this->load->model('Menu_model','menu');
		
		//获取所有菜单，并以id为下标，菜单名称为值返回数据
		$all_menu = $this->menu->getField(array(), 'id,title', true);
		
		$total = $this->menu->getCount($where);//获取数据记录数
		$list = $this->menu->getData(0,$where);//获取数据
		if ( $list ){
			foreach ( $list as $key => $val ){
				if ( $val->pid ){
					$list[$key]->up_title = $all_menu[$val->pid];
				}	
			}
		}	
		$this->load->vars('record_count', $total);
		$this->load->vars('list', $list);
		$this->load->vars('meta_title','菜单管理');
		$this->load->view('menu/index');
	}
	
	/**
	 * 新建菜单
	 * @author Baip
	 */
	public function create(){
		$pid = intval( $this->uri->segment(3) );
		//引入表单验证类
		$this->load->library('form_validation');
		//验证表单及是post提交的时候才可以入库
		if( $this->form_validation->run('menu') && IS_POST){
			$this->load->model('Menu_model','menu');//载入Menu模型
			$insert_id = $this->menu->addData();
			if ( $insert_id ){
				$this->show_message('success','添加成功');
			}else{
				$this->show_message('error','添加失败');
			}
		
		}else{
			$menus = $this->getMenutoTree();
			$this->load->vars('meta_title','添加菜单');
			$this->load->vars('pid', $pid);
			$this->load->vars('menus', $menus);
			$this->load->view('menu/create');
		}	
	}
	
	/**
	 * 获取菜单的树形结构
	 * @author Baip
	 */
	public function getMenutoTree(){
		$where = array();
		$this->load->model('Menu_model','menu');//载入Menu模型类
		$menus = $this->menu->getData(1,$where,'title,id,pid');
		$this->load->library('Tree');//载入第三方树形结构类
		$menus = $this->tree->toFormatTree($menus);
		$menus = array_merge(array(0=>array('id'=>0,'title_show'=>'顶级菜单')), $menus);
		return $menus;
		
	}
	
	/**
	 * 修改菜单
	 */
	public function modfiy(){
		$id = intval( $this->uri->segment(3) ) == 0 ? $this->input->get_post('id') : intval( $this->uri->segment(3) );
		if ( $id <= 0 ){
			$this->show_message('error', '参数错误', site_url('menu/index'));
		}
		
		$this->load->library('form_validation');
		//验证表单及是post提交的时候才可以入库
		if( $this->form_validation->run('menu') && IS_POST){
			$this->load->model('Menu_model', 'menu');
			$update_status = $this->menu->updateDataByPostData(array('id'=>$id));
			if( $update_status ){
				$this->show_message('success','操作成功', site_url('menu/index'));
			}else{
				$this->show_message('error','操作失败', site_url('menu/modfiy/'.$id));
			}
		
		}else{
			//获取菜单详情
			$this->load->model('Menu_model', 'menu');//载入Menu模型
			$menu_info = $this->menu->getFind(1, array('id'=>$id),"*");
			
			$menus = $this->getMenutoTree();//获取菜单树形结构
			$this->load->vars('meta_title','修改菜单');
			$this->load->vars('menus', $menus);
			$this->load->vars('menu_info', $menu_info);
			$this->load->view('menu/create');
		}
	}
	
	/**
	 * 删除分类信息
	 * @author Baip
	 */
	public function del(){
		$id = intval( $this->input->get_post('id',TRUE) );
		if ( 0 == $id || !is_numeric( $id ) ){
			ajaxreturn(0,'参数错误', site_url('catgory/index'));
		}
		//TODO检测是此分类下是否有数据，如果有数据不能删除
	
		$this->load->model('Menu_model','menu');//载入catgory模型类
		$state = $this->menu->deleteData('id', $id);
		if ( $state ) {
			ajaxreturn(1,'操作成功', site_url('catgory/index'));
		}else{
			ajaxreturn(0,'参数错误', site_url('catgory/index'));
		}
	}
	
	/**
	 * 自定义排序菜单
	 */
	public function sort(){
		if ( IS_POST ){
				$sorts = $this->input->post('sort');
				if(!is_array($sorts)){
					$this->show_message('success','参数错误!');
				}
				$this->load->model('Menu_model', 'menu');
				
				foreach ($sorts as $id => $sort) {
					$this->menu->updateData( intval($id), 2, 'id' , array('sort'=> intval($sort))  );
				}
				//记录用户行业
				$this->show_message('error','更新完成！', site_url('menu/index'));
		}else{
			$this->show_message('success','非法请求');
		}	
			
	}
	
}